﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Raise3000.Code.Foundation
{
    public enum StreetEnum
    {
        eUNKNOWNSTREET,
        ePREFLOP,
        eFLOP,
        eTURN,
        eRIVER
    };

    public class HandContext
    {

        private long numberOfSeats_ = -1;

        private StreetEnum street_;
        private double currentPot_ = 0;
        
        private long dealerPosition_ = -1;

        // Community cards.
        private Card[] flop_ = null;
        private Card turn_ = null;
        private Card river_ = null;

        // The seats are numbered from 0.
        private List<string> currentPlayers_ = null;
        private List<double> currentStacks_ = null;

        private List<PlayerAction> actionsOnPreflop_ = new List<PlayerAction>();
        private List<PlayerAction> actionsOnFlop_ = new List<PlayerAction>();
        private List<PlayerAction> actionsOnTurn_ = new List<PlayerAction>();
        private List<PlayerAction> actionsOnRiver_ = new List<PlayerAction>();

        // attributes
        public StreetEnum Street
        {
            get
            {
                return street_;
            }
            set
            {
                street_ = value;
            }
        }

        public double CurrentPot
        {
            get
            {
                return currentPot_;
            }
            set
            {
                currentPot_ = value;
            }
        }

        public long DealerPosition
        {
            get
            {
                return dealerPosition_;
            }
            set
            {
                dealerPosition_ = value;
            }
        }

        public List<string> GetCurrentPlayers()
        {
            return currentPlayers_;
        }

        // ctors.
        public HandContext(int numberOfSeats)
        {
            numberOfSeats_ = numberOfSeats;
            currentPlayers_ = new List<string>();
            currentStacks_ = new List<double>();

            // #WorkAround: Fill with dummy values.
            for (int idx = 0; idx < numberOfSeats; ++idx)
            {
                currentPlayers_.Add("dummy");
                currentStacks_.Add(-6.6); // dummy value.
            }
        }

        public void Initialize(string playerName, int position, double stack)
        {
            currentPlayers_[position-1] = playerName;
            currentStacks_[position-1] = stack;
        }

        public void SetFlop(Card[] flop)
        {
            flop_ = flop;
        }

        public void SetTurn(Card turn)
        {
            turn_ = turn;
        }

        public void SetRiver(Card river)
        {
            river_ = river;
        }

        public void AddToPreflop(PlayerAction m)
        {
            actionsOnPreflop_.Add(m);
        }

        public void AddToFlop(PlayerAction m)
        {
            actionsOnFlop_.Add(m);
        }

        public void AddToTurn(PlayerAction m)
        {
            actionsOnTurn_.Add(m);
        }

        public void AddToRiver(PlayerAction m)
        {
            actionsOnRiver_.Add(m);
        }

        public void AddToPot(double amount)
        {
            currentPot_ += amount;
        }       

        // Access to the action's list.
        public List<PlayerAction> GetActions(StreetEnum byStreet)
        {
            List<PlayerAction> actions = null;

            switch (byStreet)
            {
                case StreetEnum.ePREFLOP:
                    {
                        actions = actionsOnPreflop_;
                        break;
                    }
                case StreetEnum.eFLOP:
                    {
                        actions = actionsOnFlop_;
                        break;
                    }
                case StreetEnum.eTURN:
                    {
                        actions = actionsOnTurn_;
                        break;
                    }
                case StreetEnum.eRIVER:
                    {
                        actions = actionsOnRiver_;
                        break;
                    }
            };

            return actions;
        }

    }
}
